package kr.pe.johnkim.chattingday.mobile.rest.controller;

import kr.pe.johnkim.chattingday.mobile.domain.ChattingDayGathering;
import kr.pe.johnkim.chattingday.mobile.rest.service.ChattingDayGatheringService;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Chatting Day 채팅방 controller 클래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 6. 5
 */

@Controller
@Path("/chattingDayGatheringController")
public class ChattingDayGatheringController {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(ChattingDayGatheringController.class);

    @Autowired
    private ChattingDayGatheringService chattingDayGatheringService = null;

    /**
     * 새로운 채팅방을 등록 한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/registerGathering")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public ChattingDayGathering registerGathering(InputStream is) throws Exception {
        logger.info("registerGathering");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonGathering = new JSONObject(line);
        JSONObject jsonGathering2 = jsonGathering.getJSONObject("chattingDayGathering");

        ChattingDayGathering parameterGathering = new ChattingDayGathering();

        parameterGathering.setId(jsonGathering2.getString("id"));
        parameterGathering.setOwner(jsonGathering2.getString("owner"));
        parameterGathering.setSubject(jsonGathering2.getString("subject"));
        parameterGathering.setContent(jsonGathering2.getString("content"));
        parameterGathering.setFileUrl(jsonGathering2.getString("fileUrl"));
        parameterGathering.setOpenYn(jsonGathering2.getString("openYn"));
        parameterGathering.setMaxParticipants(jsonGathering2.getString("maxParticipants"));
        parameterGathering.setPassword(jsonGathering2.getString("password"));
        parameterGathering.setSpamYn(jsonGathering2.getString("spamYn"));
        parameterGathering.setActivateYn(jsonGathering2.getString("activateYn"));
        parameterGathering.setModifyDate(jsonGathering2.getString("modifyDate"));
        parameterGathering.setRegisterDate(jsonGathering2.getString("registerDate"));

        logger.info("id              : {}", parameterGathering.getId());
        logger.info("owner           : {}", parameterGathering.getOwner());
        logger.info("subject         : {}", parameterGathering.getSubject());
        logger.info("content         : {}", parameterGathering.getContent());
        logger.info("fileUrl         : {}", parameterGathering.getFileUrl());
        logger.info("openYn          : {}", parameterGathering.getOpenYn());
        logger.info("maxParticipants : {}", parameterGathering.getMaxParticipants());
        logger.info("password        : {}", parameterGathering.getPassword());
        logger.info("spamYn          : {}", parameterGathering.getSpamYn());
        logger.info("activateYn      : {}", parameterGathering.getActivateYn());
        logger.info("modifyDate      : {}", parameterGathering.getModifyDate());
        logger.info("registerDate    : {}", parameterGathering.getRegisterDate());

        ChattingDayGathering resultGathering = new ChattingDayGathering();

        if(chattingDayGatheringService.registerGathering(parameterGathering)) {
            resultGathering.setResult("SUCCESS");
        } else {
            resultGathering.setResult("FAIL");
        }

        return resultGathering;
    }

    /**
     * 채팅방 정보를 가져 온다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectGatheringInformation")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public ChattingDayGathering selectGatheringInformation(InputStream is) throws Exception {
        logger.info("selectGatheringInformation");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonGathering = new JSONObject(line);
        JSONObject jsonGathering2 = jsonGathering.getJSONObject("chattingDayGathering");

        ChattingDayGathering parameterGathering = new ChattingDayGathering();

        parameterGathering.setIdChattingDayGatherings(jsonGathering2.getString("idChattingDayGatherings"));
        parameterGathering.setActivateYn(jsonGathering2.getString("activateYn"));

        logger.info("idChattingDayGatherings : {}", parameterGathering.getIdChattingDayGatherings());
        logger.info("activateYn              : {}", parameterGathering.getActivateYn());

        return chattingDayGatheringService.selectGatheringInformation(parameterGathering);
    }

    /**
     * 검색한 채팅방 목록을 가져 온다.
     * (검색된 채팅방의 총 갯수는 검색된 목록을 크기로 계산 한다.)
     *
     * 검색 조건은 추후 완성할 것.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectSearchGatheringList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<ChattingDayGathering> selectSearchGatheringList(InputStream is) throws Exception {
        logger.info("selectSearchGatheringList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonGathering = new JSONObject(line);
        JSONObject jsonGathering2 = jsonGathering.getJSONObject("chattingDayGathering");

        ChattingDayGathering parameterGathering = new ChattingDayGathering();

        return chattingDayGatheringService.selectSearchGatheringList(parameterGathering);
    }

    /**
     * 채팅방 정보를 수정 한다.
     *
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/updateGatheringInformation")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public ChattingDayGathering updateGatheringInformation(InputStream is) throws Exception {
        logger.info("updateGatheringInformation");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonGathering = new JSONObject(line);
        JSONObject jsonGathering2 = jsonGathering.getJSONObject("chattingDayGathering");

        ChattingDayGathering parameterGathering = new ChattingDayGathering();

        parameterGathering.setIdChattingDayGatherings(jsonGathering2.getString("idChattingDayGatherings"));
        parameterGathering.setId(jsonGathering2.getString("id"));
        parameterGathering.setOwner(jsonGathering2.getString("owner"));
        parameterGathering.setSubject(jsonGathering2.getString("subject"));
        parameterGathering.setContent(jsonGathering2.getString("content"));
        parameterGathering.setFileUrl(jsonGathering2.getString("fileUrl"));
        parameterGathering.setOpenYn(jsonGathering2.getString("openYn"));
        parameterGathering.setMaxParticipants(jsonGathering2.getString("maxParticipants"));
        parameterGathering.setPassword(jsonGathering2.getString("password"));
        parameterGathering.setActivateYn(jsonGathering2.getString("activateYn"));
        parameterGathering.setModifyDate(jsonGathering2.getString("modifyDate"));
        parameterGathering.setRegisterDate(jsonGathering2.getString("registerDate"));

        logger.info("idChattingDayGatherings : {}", parameterGathering.getIdChattingDayGatherings());
        logger.info("id                      : {}", parameterGathering.getId());
        logger.info("owner                   : {}", parameterGathering.getOwner());
        logger.info("subject                 : {}", parameterGathering.getSubject());
        logger.info("content                 : {}", parameterGathering.getContent());
        logger.info("fileUrl                 : {}", parameterGathering.getFileUrl());
        logger.info("openYn                  : {}", parameterGathering.getOpenYn());
        logger.info("maxParticipants         : {}", parameterGathering.getMaxParticipants());
        logger.info("password                : {}", parameterGathering.getPassword());
        logger.info("activateYn              : {}", parameterGathering.getActivateYn());
        logger.info("modifyDate              : {}", parameterGathering.getModifyDate());

        ChattingDayGathering resultGathering = new ChattingDayGathering();

        if(chattingDayGatheringService.updateGatheringInformation(parameterGathering)) {
            resultGathering.setResult("SUCCESS");
        } else {
            resultGathering.setResult("FAIL");
        }

        return resultGathering;
    }

    /**
     * 채팅방을 비활성화 한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/updateDeactivateGathering")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public ChattingDayGathering updateDeactivateGathering(InputStream is) throws Exception {
        logger.info("updateDeactivateGathering");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonGathering = new JSONObject(line);
        JSONObject jsonGathering2 = jsonGathering.getJSONObject("chattingDayGathering");

        ChattingDayGathering parameterGathering = new ChattingDayGathering();

        parameterGathering.setIdChattingDayGatherings(jsonGathering2.getString("idChattingDayGatherings"));
        parameterGathering.setId(jsonGathering2.getString("id"));
        parameterGathering.setActivateYn(jsonGathering2.getString("activateYn"));
        parameterGathering.setModifyDate(jsonGathering2.getString("modifyDate"));

        logger.info("idChattingDayGatherings : {}", parameterGathering.getIdChattingDayGatherings());
        logger.info("id                      : {}", parameterGathering.getId());
        logger.info("activateYn              : {}", parameterGathering.getActivateYn());
        logger.info("modifyDate              : {}", parameterGathering.getModifyDate());

        ChattingDayGathering resultGathering = new ChattingDayGathering();

        if(chattingDayGatheringService.updateDeactivateGathering(parameterGathering)) {
            resultGathering.setResult("SUCCESS");
        } else {
            resultGathering.setResult("FAIL");
        }

        return resultGathering;
    }

    /**
     * 개설된 채팅방의 총 갯수를 가져 온다.
     *
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectGatheringCount")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public ChattingDayGathering selectGatheringCount(InputStream is) throws Exception {
        logger.info("selectGatheringCount");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonGathering = new JSONObject(line);
        JSONObject jsonGathering2 = jsonGathering.getJSONObject("chattingDayGathering");

        ChattingDayGathering parameterGathering = new ChattingDayGathering();

        parameterGathering.setActivateYn(jsonGathering2.getString("activateYn"));

        logger.info("activateYn : {}", parameterGathering.getActivateYn());

        return chattingDayGatheringService.selectGatheringCount(parameterGathering);
    }
}
